package haust;


public class Timer
{
	private long prevTime;
	private long time;
	private long deltaTime;
	private double delta;
	
	/**
	 * Initializes values and sets the framelock. 
	 * Must be >0, else it defaults to 60
	 * 
	 * @param fps Which fps to lock in on
	 */
	public Timer(int fps)
	{
		prevTime = System.nanoTime();
		time = prevTime;
		delta = 0.0;
		
		if( fps <= 0 )
			fps = 60; //default to 60
		
		deltaTime = 1000000000 / fps;
	}

	/**
	 * 
	 * @return true if 1/fps has been passed since last checkTime() has returned true.
	 */
	public boolean checkTime()
	{
		time = System.nanoTime();
		
		long d = time - prevTime;
		if( d > deltaTime )
		{
			delta = ((double) d / 1000000000.0);
			prevTime = time;
			return true;
		}
		
		return false;
	}
	
	/**
	 * 
	 * @return The amount in seconds since last frame
	 */
	public float getDelta()
	{
		return (float)delta;
	}
}